package cn.dlust.tt.module.teacher.controller.admin.courses;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.dlust.tt.framework.common.pojo.PageParam;
import cn.dlust.tt.framework.common.pojo.PageResult;
import cn.dlust.tt.framework.common.pojo.CommonResult;
import cn.dlust.tt.framework.common.util.object.BeanUtils;
import static cn.dlust.tt.framework.common.pojo.CommonResult.success;

import cn.dlust.tt.framework.excel.core.util.ExcelUtils;

import cn.dlust.tt.framework.apilog.core.annotation.ApiAccessLog;
import static cn.dlust.tt.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.dlust.tt.module.teacher.controller.admin.courses.vo.*;
import cn.dlust.tt.module.teacher.dal.dataobject.courses.CoursesDO;
import cn.dlust.tt.module.teacher.service.courses.CoursesService;

@Tag(name = "管理后台 - 课程")
@RestController
@RequestMapping("/teacher/courses")
@Validated
public class CoursesController {

    @Resource
    private CoursesService coursesService;

    @PostMapping("/create")
    @Operation(summary = "创建课程")
    @PreAuthorize("@ss.hasPermission('teacher:courses:create')")
    public CommonResult<Long> createCourses(@Valid @RequestBody CoursesSaveReqVO createReqVO) {
        return success(coursesService.createCourses(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新课程")
    @PreAuthorize("@ss.hasPermission('teacher:courses:update')")
    public CommonResult<Boolean> updateCourses(@Valid @RequestBody CoursesSaveReqVO updateReqVO) {
        coursesService.updateCourses(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除课程")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('teacher:courses:delete')")
    public CommonResult<Boolean> deleteCourses(@RequestParam("id") Long id) {
        coursesService.deleteCourses(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得课程")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('teacher:courses:query')")
    public CommonResult<CoursesRespVO> getCourses(@RequestParam("id") Long id) {
        CoursesDO courses = coursesService.getCourses(id);
        return success(BeanUtils.toBean(courses, CoursesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得课程分页")
    @PreAuthorize("@ss.hasPermission('teacher:courses:query')")
    public CommonResult<PageResult<CoursesRespVO>> getCoursesPage(@Valid CoursesPageReqVO pageReqVO) {
        PageResult<CoursesDO> pageResult = coursesService.getCoursesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CoursesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出课程 Excel")
    @PreAuthorize("@ss.hasPermission('teacher:courses:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCoursesExcel(@Valid CoursesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CoursesDO> list = coursesService.getCoursesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "课程.xls", "数据", CoursesRespVO.class,
                        BeanUtils.toBean(list, CoursesRespVO.class));
    }

}